import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.0
import QtQuick.Dialogs 1.0

import "./common/"

Item {
    id: root
    width: gWidth
    height: gHeight

    property var _OBJS_POOL: new Array

    property real moveXTime: 0
    property real moveYTime: 0

    property real gWidth: 800
    property real gHeight: 1000

    property bool isEdit: false

    property string lastStr: ""
    property real columnMax: 12   // 一行最大字数
    property real funciontIndex: 0  // 0: 卡片颜色, 1: 字体颜色, 2: 生成

    property color globalColor: Qt.rgba(104/255, 71/255, 40/255, 1)
    property color globalbgColor: Qt.rgba(228/255, 150/255, 93/255, 1)


    property real gLineIndex: 0         // 解析文字行数
    property real gInLineCounter: 0     // 一行内解析文字进度
    property real gStrIndex: 0          // 整个字符串解析进度

    function generateManByTime()
    {
        if(lastStr === "") return
        if(gStrIndex >= lastStr.length) return

        if(lastStr[gStrIndex] === "\n")
        {
            console.log("generateManByTime \\n 换行")
            gLineIndex++
            gInLineCounter = 0
        }
        else{
            createMan(gInLineCounter%columnMax, gLineIndex, lastStr[gStrIndex])
            if((gInLineCounter+1) % columnMax === 0)
            {
                // 换行，重置计数
                gInLineCounter = 0
                gLineIndex++
            }
            else{
                gInLineCounter++
            }
        }
    }

    function generateMan(value)
    {
        console.log("generateMan ", value)
        if(lastStr.length > 100)
        {
            console.log("文字超过100个")
            return
        }

        if(timer.running)
        {
            console.log("正常生成小人")
            return
        }

        if(lastStr === value)
        {
            return; // 防止重复生成一样的
        }
        lastStr = value

        if(_OBJS_POOL.length > 0)
        {
            // 清除
            var len = _OBJS_POOL.length
            for(var i = len-1; i >= 0; --i)
            {
                var obj = _OBJS_POOL[i]
                obj.destroy()
                _OBJS_POOL.splice(i, 1)
            }
            _OBJS_POOL = new Array
        }

        len = lastStr.length
        gLineIndex = gInLineCounter = gStrIndex = 0

//        for(var i = 0; i < len; ++i)
//        {
//            generateManByTime()
//            gStrIndex++
//        }
        timer.restart()
    }

    function createMan(ix, iy, txt)
    {
//        40+51.5*(i%maxNum)+line*12.5, 149+18.5*(i%maxNum)+line*65
        var posx = 40+51.5*(ix)+iy*12.5
        var posy = 149+18.5*(ix)+iy*65
        createInstance(posx, posy, txt)
    }

    function createInstance(startx, starty, txt)
    {
        var obj = Qt.createComponent("common/MySmallMan.qml").createObject(layerMan)

        obj.virtualX = startx
        obj.virtualY = starty
        obj.source = "qrc:/image/image/girl" + String(1+getRandom(10)) + ".png"
        obj.txt = txt
        _OBJS_POOL.push(obj)
    }

    function getRandom(value)
    {
        Math.seed = (new Date).toLocaleString()
        var r = Math.random()
        var index = Math.floor(value * r)
        return index
    }

    Connections{
        target: widget
        onNotifyMsg:
        {
//            createInstance(mx, my)
        }
    }

    function test()
    {
//        1行x: 43->space: 54; y:149->space: 21
//         2行x: space (43) 15, y: 65
        var line = 0
        var maxNum = 12
        for(var i = 0; i < 13; i++)
        {
             console.log(i)
            createMan(i%maxNum, line, String(i))
//            createInstance(40+51.5*(i%maxNum)+line*12.5, 149+18.5*(i%maxNum)+line*65)
            // 一样3个
            if((i+1) % maxNum === 0)
            {
                // 换行
                console.log("//   换行")
                line++
            }
        }
    }

    Component.onCompleted:
    {
//        test()
        generateMan(textEdit.text)
    }

    Timer{
        id: timer
        repeat: true
        interval: 50
        onTriggered: {
            if(gStrIndex >= lastStr.length)
            {
                timer.stop()
                return
            }
            generateManByTime()
            gStrIndex++
        }
    }

    Rectangle{
        id: itemShot
        anchors.fill: parent

        // 竖线
        RotateBase{
            id: itemGrid
            width: Screen.width
            height: Screen.height
            x: width/2
            y: -height
            rAngle: 40

            // 一行18个格子
            Row{
                id: row
                spacing: 50

                Repeater{
                    model: 50
                    Rectangle { color: Qt.rgba(200/255, 200/255, 200/255, 1); width: 1; height: Screen.width*2; }
                }
            }
        }

        // 横线
        RotateBase{
            id: itemGrid2
            width: itemGrid.width
            height: itemGrid.height
            y: -height
            rAngle: 20

            Column{
                id: row2
                spacing: 55

                Repeater{
                    model: 50
                    Rectangle { color: Qt.rgba(200/255, 200/255, 200/255, 1); width: Screen.width*2; height: 1; }
                }
            }
        }

        Item{
            id: layerMan
            anchors.fill: parent
        }
    }

    /*
//    MySmallMan{
//        id: t
//        z: 1111
//        virtualX: 200
//        virtualY: 200
//        source: "qrc:/image/image/boy1.png"
//        txt: "你"
////        xAngle: s1.v
////        yAngle: s2.v
////        zAngle: s3.v

//        Rectangle{
//            anchors.fill: parent
//            color: "red"
//            opacity: 0.3
//        }
//    }

//    //test
//    Column{
//        Bar{
//            id: s1
//            name: qsTr("X轴: ")
//        }
//        Bar{
//            id: s2
//            name: qsTr("Y轴: ")
//        }
//        Bar{
//            id: s3
//            name: qsTr("Z轴: ")
//        }
//    }*/

    Item{
        id: itemEdit
        anchors.fill: parent

        Row{
            spacing: 5
            anchors.right: parent.right
            anchors.top: parent.top
            z: 1

            Button{
                text: "截图"
                onClicked: {
                    itemShot.grabToImage(function(result) {
                        result.saveToFile("screenshots.png");
                    })
                }
            }

            Button{
                id: b1
                text: "卡片颜色"
                onClicked: {
                    funciontIndex = 0
                    colorDialog.visible = true
                }
            }

            Button{
                id: b2
                text: "字体颜色"
                onClicked: {
                    funciontIndex = 1
                    colorDialog.visible = true
                }
            }
            Button{
                text: isEdit ? "生成" : "编辑"
                onClicked: {
                    funciontIndex = 2
                    if(isEdit)
                    {
                        // 编辑->生成
                        generateMan(textEdit.text.trim())
                        b1.enabled = b2.enabled = true
                    }
                    else{
                        // 生成->编辑
                        b1.enabled = b2.enabled = false
                    }

                    isEdit = isEdit ? false : true
                }

                Image {
                    visible: isEdit
                    source: "qrc:/image/image/arrow.png"
                    anchors.horizontalCenter: parent.horizontalCenter
                    SequentialAnimation on y {
                        running: isEdit
                        loops: -1
                        NumberAnimation{ from: 40; to: 30; duration: 500; }
                        NumberAnimation{ to: 40; duration: 500; }
                    }
                }
            }
        }

        Rectangle{
            width: parent.width-100
            height: parent.height-400
            anchors.centerIn: parent
            color: "lightgray"
            visible: isEdit
            radius: 5

            Rectangle{
                width: parent.width-22
                height: parent.height-22
                anchors.centerIn: parent
                radius: 5

                TextEdit{
                    id: textEdit
                    anchors.fill: parent
                    focus: true
                    font.pixelSize: 20
                    selectByKeyboard: true
                    selectByMouse: true
                    wrapMode: TextEdit.WordWrap
                    text: "写些想说的话"
                }
            }
        }

    }

    ColorDialog {
        id: colorDialog
        title: "选择一种颜色"
        visible: false
        modality: Qt.ApplicationModal
        onAccepted: {
            console.log("You chose: " + colorDialog.color)
            if(funciontIndex === 0)
            {
                globalbgColor = colorDialog.color
            }
            else if(funciontIndex === 1)
            {
                globalColor = colorDialog.color
            }
        }
        onRejected: {
            console.log("Canceled")
        }
    }

}
